package main

import (
	"flag"
	"fmt"
	"ja-netfilter-plugin-util/kernel"
	_ "ja-netfilter-plugin-util/kernel"
)

var mode = flag.String("m", "", "任务执行模式: \n - write 写入javaagent信息 [default]\n - clear 清空javaagent信息")

func main() {
	defer kernel.Exit()
	flag.Parse()
	if *mode == "" {
		flag.PrintDefaults()
		return
	}
	appList, err := kernel.GetAllJetbrainsApplication()
	if err != nil {
		return
	}
	var modeFunc func(application string, vmFilePath string) error
	switch *mode {
	case "write":
		modeFunc = kernel.WriteVmOptions
	case "clear":
		modeFunc = kernel.RemoveJavaagent
	default:
		fmt.Println(fmt.Errorf("不支持的mode:%q", *mode))
	}
	for _, item := range appList {
		vmFilePath, err := kernel.GetVmOptionsFile(item)
		if err != nil {
			continue
		}
		err = modeFunc(item, vmFilePath)
	}
}
